/*
 * Copyright (c) Contributors, http://openviewer.org/
 * See CONTRIBUTORS.TXT for a full list of copyright holders.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenViewer Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Imaging;
using Mogre;
using ThirdLife.Render.MogrePlugin;

namespace ThirdLife.Render.MogrePlugin
{
  public class OgreUtil
  {
    public static CustomShape CreateTestObject()
    {
      CustomShape mShape = new CustomShape();
      mShape.AddPoint(new Vector3(0, 0, 0));
      mShape.AddTexturePoint(new Vector3(0, 0, 0));

      mShape.AddPoint(new Vector3(100, 0, 0));
      mShape.AddTexturePoint(new Vector3(1, 0, 0));

      mShape.AddPoint(new Vector3(100, 100, 0));
      mShape.AddTexturePoint(new Vector3(1, 1, 0));

      mShape.SetMaterial("Examples/EnvMappedRustySteel");
      mShape.Draw();
      return mShape;
    }

    public static SceneNode CreatePlane(SceneManager sceneMgr, string name, string material, float x, float y, float z, float sizex, float sizez, int xseg, int yseg, float tileu, float tilev)
    {
      Plane plane1 = new Plane();
      plane1.normal = new Vector3(0, 1, 0);
      plane1.d = 0.1f;

      MeshPtr floorMesh = MeshManager.Singleton.CreatePlane(name + "1", "", plane1, sizex, sizez, xseg, yseg, true, 1, tileu, tilev, Vector3.UNIT_Z);
      Entity floorEntity = sceneMgr.CreateEntity(name, name + "1");
      floorEntity.SetMaterialName(material);
      SceneNode floorNode = (SceneNode)sceneMgr.RootSceneNode.CreateChildSceneNode();
      floorNode.AttachObject(floorEntity);
      floorNode.Position = new Vector3(x, y, z);

      return floorNode;
    }


    public static Light CreateLight(SceneManager sceneMgr, int type, string name, Vector3 pos, Vector3 direction)
    {
      Light light = sceneMgr.CreateLight(name);
      if (type == 1)
      {
        light.Type = Light.LightTypes.LT_SPOTLIGHT;
      }
      else if (type == 2)
      {
        light.Type = Light.LightTypes.LT_DIRECTIONAL;
      }
      else
      {
        light.Type = Light.LightTypes.LT_POINT;
      }
      light.SetDiffuseColour(1, 1, 1);
      light.SetSpecularColour(1, 1, 1);
      light.CastShadows = true;
      light.Position = pos;
      light.Direction = direction;

      light.Visible = true;

      if (type == 1)
      {
        //light.SpotlightFalloff =45.0f;
        light.SetSpotlightRange(new Degree(35), new Degree(50));
      }
      return light;
    }

    public static string GetTerrainString(float x, float z, string heightmap, string material)
    {
      string terrainString;

      terrainString = "PageSource=Heightmap" + Environment.NewLine;
      terrainString += "Heightmap.image=" + heightmap + Environment.NewLine;
      // terrainString += "Heightmap.raw.size=513";
      // terrainString += "Heightmap.raw.bpp=2";
      terrainString += "PageSize=257" + Environment.NewLine;
      terrainString += "TileSize=129" + Environment.NewLine;
      terrainString += "MaxPixelError=2" + Environment.NewLine;
      terrainString += "PageWorldX=" + x.ToString() + Environment.NewLine;
      terrainString += "PageWorldZ=" + z.ToString() + Environment.NewLine;
      terrainString += "MaxHeight=60" + Environment.NewLine;
      terrainString += "MaxMipMapLevel=5" + Environment.NewLine;
      terrainString += "VertexNormals=yes" + Environment.NewLine;
      terrainString += "VertexProgramMorph=yes" + Environment.NewLine;
      terrainString += "LODMorphStart=0.2" + Environment.NewLine;
      terrainString += "CustomMaterialName= " + material;

      return terrainString;
    }

    public static void CreateTerrainMaterial(string materialName, string baseTextureImage, float baseTextureScale, int numberSplats, string texture1Alpha, string texture1Image, float texture1Scale, string texture2Alpha, string texture2Image, float texture2Scale)
    {

      MaterialPtr material = MaterialManager.Singleton.Create(materialName, ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME);
      TextureUnitState textureUnit = material.GetTechnique(0).GetPass(0).CreateTextureUnitState(baseTextureImage);   //("grass_1024.jpg");
      textureUnit.SetTextureScale(baseTextureScale, baseTextureScale);//(0.5f,0.5f);

      if (numberSplats > 0)
      {
        material.GetTechnique(0).GetPass(0).LightingEnabled = false;

        material.GetTechnique(0).CreatePass();
        material.GetTechnique(0).GetPass(1).DepthFunction = CompareFunction.CMPF_EQUAL;
        material.GetTechnique(0).GetPass(1).SetSceneBlending(SceneBlendType.SBT_TRANSPARENT_ALPHA);
        // material.GetTechnique(0).GetPass(1).LightingEnabled = false;
        textureUnit = material.GetTechnique(0).GetPass(1).CreateTextureUnitState(texture1Alpha);//("alpha_dirt.png");
        textureUnit.SetAlphaOperation(LayerBlendOperationEx.LBX_SOURCE1, LayerBlendSource.LBS_TEXTURE, LayerBlendSource.LBS_TEXTURE);
        textureUnit.SetColourOperationEx(LayerBlendOperationEx.LBX_SOURCE2, LayerBlendSource.LBS_TEXTURE, LayerBlendSource.LBS_TEXTURE);
        textureUnit = material.GetTechnique(0).GetPass(1).CreateTextureUnitState(texture1Image);//("bark-256x256.jpg");
        textureUnit.SetTextureScale(texture1Scale, texture1Scale);//(0.1f,0.1f);
        textureUnit.SetColourOperationEx(LayerBlendOperationEx.LBX_BLEND_DIFFUSE_ALPHA, LayerBlendSource.LBS_TEXTURE, LayerBlendSource.LBS_CURRENT);
        material.GetTechnique(0).GetPass(1).LightingEnabled = false;
      }
      if (numberSplats > 1)
      {
        material.GetTechnique(0).CreatePass();
        material.GetTechnique(0).GetPass(2).DepthFunction = CompareFunction.CMPF_EQUAL;
        material.GetTechnique(0).GetPass(2).SetSceneBlending(SceneBlendType.SBT_TRANSPARENT_ALPHA);
        // material.GetTechnique(0).GetPass(2).LightingEnabled = false;
        textureUnit = material.GetTechnique(0).GetPass(2).CreateTextureUnitState(texture2Alpha);//("alpha_dirt2.png");
        textureUnit.SetAlphaOperation(LayerBlendOperationEx.LBX_SOURCE1, LayerBlendSource.LBS_TEXTURE, LayerBlendSource.LBS_TEXTURE);
        textureUnit.SetColourOperationEx(LayerBlendOperationEx.LBX_SOURCE2, LayerBlendSource.LBS_TEXTURE, LayerBlendSource.LBS_TEXTURE);
        textureUnit = material.GetTechnique(0).GetPass(2).CreateTextureUnitState(texture2Image);//("Grass.jpg");
        textureUnit.SetTextureScale(texture2Scale, texture2Scale);//(0.1f,0.1f);
        textureUnit.SetColourOperationEx(LayerBlendOperationEx.LBX_BLEND_DIFFUSE_ALPHA, LayerBlendSource.LBS_TEXTURE, LayerBlendSource.LBS_CURRENT);
        material.GetTechnique(0).GetPass(2).LightingEnabled = false;
      }

      material.Dispose();
      material = null;
    }


    public static ImageCodecInfo GetImageEncoder(string imageType)
    {
      imageType = imageType.ToUpperInvariant();

      foreach (ImageCodecInfo info in ImageCodecInfo.GetImageEncoders())
      {
        if (info.FormatDescription == imageType)
        {
          return info;
        }
      }
      return null;
    }
  }
}